/**
  * Author: Mei
  * Date: 2020-11-20 14:10
  * Description: 
  *
  */
//#include <iostream>
#include <vector>
#include <chrono>
#include <random>

#include "ThreadPool.h"
std::mutex queue_mutex;
void simulate_hard_computation() {
    std::this_thread::sleep_for(std::chrono::seconds(2));
}
int multiply_return(const int a, const int b) {
    std::unique_lock<std::mutex> lock(queue_mutex);
    //simulate_hard_computation();
    const int res = a * b;
    std::cout << a << " * " << b << " = " << res << std::endl;
    return res;
}
int main() {
    //用4个工作线程创建线程池
    ThreadPool pool(4);
    std::vector<std::future<int> > results;

    for (int i = 0; i < 3; ++i) {
        results.emplace_back( //加入任务
                pool.enqueue([](int a, int b) -> decltype(a){
                    std::unique_lock<std::mutex> lock(queue_mutex);
                    //simulate_hard_computation();
                    const int res = a * b;
                    std::cout << a << " * " << b << " = " << res << std::endl;
                    //return res;
                }, 22+i, 12+i)
        );
    }
    //std::this_thread::sleep_for(std::chrono::seconds(6));
    //排队并存储future
    for (auto &&result: results)
        std::cout << result.get() << ' ';
    //从结果中得到future
    std::cout << std::endl;

    return 0;
}